package org.eenie.learn.seedquartz.api;


import org.eenie.learn.seedquartz.model.TaskModel;
import org.eenie.learn.seedquartz.quartz.job.TicketJob;
import org.eenie.learn.seedquartz.quartz.manage.QuartzManager;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Configuration
@RestController
@RequestMapping("/quartz")
@CrossOrigin(origins = "*", maxAge = 3600)
public class QuartzTaskApi {

    List<TaskModel> mTasks = new ArrayList<>();

    @Autowired
    QuartzManager mQuartzManager;

    @RequestMapping(value = "task-create", method = RequestMethod.POST)
    public ApiResponse createTask(@RequestBody @Valid TaskModel task) {
        try {
            String taskId = mQuartzManager.createTask(TicketJob.class, task.getDesc(), task.getCron());
            task.setId(taskId);
            task.setTimestamp(System.currentTimeMillis());
            task.setStatus(0);
            mTasks.add(task);
            return ApiResponse.success(taskId, taskId);
        } catch (SchedulerException e) {
            e.printStackTrace();
            return ApiResponse.fail(1001, e.getMessage());
        }
    }


    @PostMapping("/task-list")
    public ApiResponse fetchTaskList() {
        return ApiResponse.success(mTasks);
    }


    @PostMapping("/task-resume")
    public void resumeTask() {

    }


    @PostMapping("/task-pause")
    public void pauseTask() {

    }


}
